package com.gaolang.controller;

import com.gaolang.pojo.Medicine;
import com.gaolang.pojo.OrderForm;
import com.gaolang.pojo.ShopCar;
import com.gaolang.pojo.User;
import com.gaolang.service.UserService;
import com.gaolang.util.MakeCertPic;
import com.gaolang.util.MedInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author DELLHL
 */
@Controller
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/")
    public String Main(Model model){

        List<Medicine> medicine_recommend = userService.selectMedicine("",4);
        List<Medicine> medicine_health = userService.selectMedicine("养生",6);
        List<Medicine> medicine_dampness = userService.selectMedicine("祛湿",6);
        List<Medicine> medicine_Injury = userService.selectMedicine("跌打损伤",4);
        model.addAttribute("medicine_recommend",medicine_recommend);
        model.addAttribute("medicine_health",medicine_health);
        model.addAttribute("medicine_dampness",medicine_dampness);
        model.addAttribute("medicine_Injury",medicine_Injury);
        return "main";
    }

    @RequestMapping("/user/to_login")
    public String login(User user,Model model,HttpSession session){
        if (userService.loginAuthentication(user)){
            session.setAttribute("username",user.getUsername());
            return "redirect:/";
        }else {
            model.addAttribute("ps_tip","用户名或密码错误");
            return "login";
        }
    }

    @RequestMapping("/user/to_register")
    public String register(User user,String code,Model model,HttpSession session){
        if (session.getAttribute("certPic").equals(code)){
            model.addAttribute("user",user);
            session.setAttribute("username",user.getUsername());
            return "information";
        }else {

            model.addAttribute("code_tip","验证码错误");
            return "register";
        }

    }

    @RequestMapping("/user/to_register2")
    public String register2(User user,Model model,HttpSession session){
        userService.registerUser(user);
        model.addAttribute("user",user);
        session.setAttribute("username",user.getUsername());
        return "redirect:/";
    }

    @RequestMapping("/checkCodeServlet")
    public void test(HttpSession session, HttpServletResponse response) throws IOException {
        MakeCertPic mcp=new MakeCertPic();
        String certPic = mcp.getCertPic(100, 50,response.getOutputStream());
        session.setAttribute("certPic",certPic);
    }


    @RequestMapping("/user/shopping_car")
    public String shoppingCar(HttpSession session,Model model){
        if ("".equals(session.getAttribute("username"))||session.getAttribute("username")==null){
            return "login";
        }else {
            int user_id = userService.selectIdByName((String) session.getAttribute("username"));
            List<Medicine> medicines = userService.selectShopCar(user_id);
            model.addAttribute("medicines",medicines);
            medicines.forEach(System.out::println);
            return "shopCar";
        }
    }

    @RequestMapping("/user/purchase")
    public String purchase(String name,Model model) throws IOException {
        Medicine medicine = userService.selectMedicine(name);
        model.addAttribute("medicine",medicine);
        model.addAttribute("page",MedInfo.getInfo(medicine.getName()));
        return "purchase";
    }

    @RequestMapping("/user/num_modify")
    public String numModify(ShopCar shopCar,HttpSession session){
        System.out.println(shopCar.toString());
        int user_id = userService.selectIdByName((String) session.getAttribute("username"));
        shopCar.setUserId(user_id);
        userService.modifyShopNum(shopCar);
        return "redirect:/user/shopping_car";
    }

    @RequestMapping("/user/add_car")
    public String add_car(ShopCar shopCar,HttpSession session){
        if ("".equals(session.getAttribute("username"))||session.getAttribute("username")==null){
            return "login";
        }else {
            int user_id = userService.selectIdByName((String) session.getAttribute("username"));
            shopCar.setUserId(user_id);
            userService.addShop(shopCar);
            return "redirect:/user/shopping_car";
        }


    }

    @RequestMapping("/user/delete_car")
    public String delete_car(Integer medId,HttpSession session){
        int user_id = userService.selectIdByName((String) session.getAttribute("username"));
        userService.deleteShop(user_id,medId);
        return "redirect:/user/shopping_car";
    }

    @RequestMapping("/user/pay")
    public String pay(int[] checkItem,HttpSession session,Model model){
        int user_id = userService.selectIdByName((String) session.getAttribute("username"));
        User user = userService.selectUserById(user_id);
        List<Medicine> medicineList = userService.selectShopCar(user_id);
        List<Medicine> medicines = new ArrayList<>();
        for (int i = 0; i < checkItem.length; i++) {
            for (Medicine medicine : medicineList) {
                if (medicine.getId()==checkItem[i]){
                    medicines.add(medicine);
                    break;
                }
            }
        }
        if ("".equals(session.getAttribute("game"))||session.getAttribute("game")==null){
            model.addAttribute("money",10);
        }else {
            model.addAttribute("money",20);
        }
        model.addAttribute("medicines",medicines);
        model.addAttribute("user",user);
        return "pay";
    }


    @RequestMapping("/user/to_pay")
    public String toPay(int[] checkItem,String payAll,HttpSession session,Model model){
        int user_id = userService.selectIdByName((String) session.getAttribute("username"));
        User user = userService.selectUserById(user_id);

        OrderForm orderForm = new OrderForm();
        List<Medicine> medicines = userService.selectShopCar(user_id);
        orderForm.setState("未发货");
        orderForm.setUserId(user_id);
        if (user.getBalance()<Double.parseDouble(payAll)){
            return "redirect:/";
        }else {
            user.setBalance(Double.parseDouble(payAll));
            userService.modifyUser(user);
            for (int i : checkItem) {
                userService.deleteShop(user_id,i);
                orderForm.setMedId(i);
                for (Medicine medicine : medicines) {
                    if (medicine.getId()==i){
                        orderForm.setQuantity(medicine.getAmount());
                        userService.orderFormAdd(orderForm);
                        break;
                    }
                }

            }
            List<OrderForm> orders = userService.order(user_id);
            User user2 = userService.selectUserById(user_id);
            model.addAttribute("orders",orders);
            model.addAttribute("money",user2.getBalance());
            return "orders";
        }

    }

    @RequestMapping("/user/recharge")
    public String Recharge(String money,HttpSession session){
        if ("".equals(session.getAttribute("username"))||session.getAttribute("username")==null){
            return "login";
        }else{
            User user = new User();
            user.setUsername((String) session.getAttribute("username"));
            user.setBalance(Double.parseDouble(money));
            userService.modifyUser2(user);
            return "redirect:/";
        }

    }


    @RequestMapping("/user/search")
    public String user_search(String name,Model model){
        List<Medicine> medicines = userService.selectByName(name);
        model.addAttribute("medicines",medicines);
        model.addAttribute("name",name);
        return "temp";
    }

    @RequestMapping("/user/game")
    public String to_play_game(HttpSession session){
        session.setAttribute("game","game");
        return "games";
    }

    @RequestMapping("/user/page2")
    public String page1(){
        return "page2";
    }
}
